//提示彈窗
import ElementUI from 'element-ui';

export const messageTips = function (str, type) {
  ElementUI.Message({
    message: str,
    type: type || 'success'
  });
}

//检查是否为空字符串
export const checkIsEmpty = function (str) {
  if (str == null) return true;
  return str.toString().replace(/(^\s*)|(\s*$)/g, "") === '';
}

//创建文件url
export const createObjectURL = file => {
  let url = "";
  if (window.createObjectURL != undefined) {
    url = window.createObjectURL(file);
  } else if (window.URL != undefined) {
    url = window.URL.createObjectURL(file);
  } else if (window.webkitURL != undefined) {
    url = window.webkitURL.createObjectURL(file);
  }
  return url;
};

//回收文件url
export const revokeObjectURL = url => {
  if (window.createObjectURL != undefined) {
    window.revokeObjectURL(url);
  } else if (window.URL != undefined) {
    window.URL.revokeObjectURL(url);
  } else if (window.webkitURL != undefined) {
    window.webkitURL.revokeObjectURL(url);
  }
};

//獲取視頻的圖片base64
export const captureVideoImage = (fileUrl, fn) => {
  let video = document.createElement("video");
  video.crossOrigin = "Anonymous";
  video.src = fileUrl;
  video.onloadeddata = () => {
    let canvas = document.createElement("canvas");
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;
    canvas.getContext("2d").drawImage(video, 0, 0, canvas.width, canvas.height);
    let url = canvas.toDataURL("image/jpg");
    fn && fn(url);
  };
};

//圖片轉換base64
export const imgToBase64 = (fileUrl, fn) => {
  let img = new Image();
  img.crossOrigin = "Anonymous";
  img.src = fileUrl;
  img.onload = () => {
    let canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    canvas.getContext("2d").drawImage(img, 0, 0, canvas.width, canvas.height);
    let url = canvas.toDataURL("image/jpg");
    fn && fn(url);
  };
};

export const timestampToDate = function (timestamp, format = true) {
  let date = new Date(timestamp * 1000);
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

  return format ? Y + M + D + h + m + s : Y + M + D;
};

// 事件解绑
export const removeEvent = function (element, eType,bol) {
	if(!element.eventSave) return false;
	let handleType = eType.split('.')[0];
	let handleName = eType.split('.')[1] || 'all';
	if(!element.eventSave[handleName + handleType]) return false;
	let handle = element.eventSave[handleName + handleType];
    if(element.addEventListener){
		bol = bol == undefined ? false : bol;
        element.removeEventListener(handleType, handle, bol);
    }else if(element.attachEvent){
        element.detachEvent("on"+handleType, handle);
    }else{
        element["on"+handleType] = null;
    }
}

//绑定事件
export const addEvent = function (element, eType, handle, bol) {
	if(!element.eventSave){
		element.eventSave = new Object;
	}
	let handleType = eType.split('.')[0];
	let handleName = eType.split('.')[1] || 'all';
	if(!handleType) return false;
	//如果原來有一樣的事件，就先解綁，然後給綁定新的事件
	if(element.eventSave[handleName + handleType] != undefined){
		removeEvent(element,eType);
	}
	element.eventSave[handleName + handleType] = handle;
	//如果支持addEventListener
	if (element.addEventListener) {
		bol = bol == undefined ? false : bol;
		element.addEventListener(handleType, handle, bol);
	} else if (element.attachEvent) {
		//如果支持attachEvent
		element.attachEvent("on" + handleType, handle);
	} else {
		//否则使用兼容的onclick绑定
		element["on" + handleType] = handle;
	}
}

//获取滚动条位置
export const getScroll = function () {
	if (window.pageYOffset != null) {
		//支持IE9 +
		return {
			left: window.pageXOffset,
			top: window.pageYOffset
		}
	} else if (document.compatMode == 'CSS1Compat') {
		//声明了DTD
		return {
			left: document.documentElement.scrollLeft,
			top: document.documentElement.scrollTop
		}
	}
	return {
		left: document.body.scrollLeft,
		top: document.body.scrollTop
	}
}